package com.collectplus.express.parcel;

import android.text.Html;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.collectplus.express.R;
import com.collectplus.express.model.Const;
import com.collectplus.express.model.ParcelReceiveWayAndSignedBean;
import com.collectplus.express.parcel.fragment.BaseReceiverParcelFragment;

import java.text.MessageFormat;
import java.util.ArrayList;

import droid.frame.activity.base.BaseAdapterExt;
import droid.frame.utils.GlideUtil;
import droid.frame.utils.lang.Str;

/**
 * Created by joker on 2018/3/8.
 * 收到的已签收包裹适配器
 */
public class ParcelReceiverSignedAdapter extends BaseAdapterExt<ParcelReceiveWayAndSignedBean> {

    private String stationParcelStr = "<font color=#C6C6C6>暂存编码：{0}（请在</font><font color=#C6C6C6>{1}号</font><font color=#C6C6C6>货架上查找该包裹）</font>";

    public ParcelReceiverSignedAdapter(ArrayList<ParcelReceiveWayAndSignedBean> items, BaseReceiverParcelFragment fragment) {
        super(items, fragment.getActivityContext());
        setEmptyView(R.layout.parcel_receiver_signed_empty);
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        if (hasEmptyView()) {
            return super.getView(position, convertView, parent);
        }
        ViewHolder holder = null;
        if (convertView == null || convertView.getTag() == null) {
            holder = new ViewHolder();
            convertView = LayoutInflater.from(context).inflate(R.layout.parcel_signed_receive_item, parent, false);
            holder.senderName = (TextView) convertView.findViewById(R.id.parcel_sender_name_tv);
            holder.senderAddress = (TextView) convertView.findViewById(R.id.parcel_sender_address_tv);
            holder.senderPhone = (TextView) convertView.findViewById(R.id.parcel_contract_phone_tv);
            holder.expressInfoTV = (TextView) convertView.findViewById(R.id.parcel_express_info_tv);
            holder.routerInfoTV = (TextView) convertView.findViewById(R.id.parcel_collect_code);
            holder.parcelTypeIV = (ImageView) convertView.findViewById(R.id.city_wide_or_other);
            holder.expressLogoIV = (ImageView) convertView.findViewById(R.id.express_logo_iv);
            holder.bottomLineV = convertView.findViewById(R.id.divde_line_v);
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }
        ParcelReceiveWayAndSignedBean item = getItem(position);
        if (Str.isNotEmpty(item.getExpressNumber())) {
            holder.expressInfoTV.setText(item.getExpressCompanyName() + "：" + item.getExpressNumber());
        } else {
            holder.expressInfoTV.setText(item.getExpressCompanyName());
        }
        if (position == getItemsSize() - 1) {
            holder.bottomLineV.setVisibility(View.INVISIBLE);
        } else {
            holder.bottomLineV.setVisibility(View.VISIBLE);
        }
        //从代收驿站过来的数据
        if (item.getType() == 2) {
            holder.senderName.setText("收件人：" + item.getReceiverName());
            holder.senderPhone.setText("联系电话：" + item.getReceiverPhone());
            holder.routerInfoTV.setText(item.getTakeTime() + " 从" + item.getStationName() + "取走");
            GlideUtil.loadImage(context, item.getExpressCompanyUrl(), 0, holder.expressLogoIV);
            if (item.isBig()) {
                holder.parcelTypeIV.setImageResource(R.drawable.take_parcel_big_disable_icon);
            } else {
                holder.parcelTypeIV.setImageResource(R.drawable.take_parcel_small_disable_icon);
            }
            holder.senderAddress.setText(Html.fromHtml(MessageFormat.format(stationParcelStr, item.getStorageCode(), item.getStorageRack())));
            holder.senderAddress.getPaint().setFakeBoldText(true);
        } else {
            //同城
            if (item.getParcelType() == Const.ParcelType.sameCityToHome) {
                holder.parcelTypeIV.setImageResource(R.drawable.take_samecity_disable_icon);
            } else {
                holder.parcelTypeIV.setImageResource(R.drawable.take_national_disable_icon);
            }
            holder.routerInfoTV.setText(item.getTakeTime() + "  完成签收");
            holder.senderName.setText("寄件人：" + item.getUserName());
            holder.senderAddress.setText("地址：" + item.getUserAddress());
            holder.senderPhone.setText("联系电话：" + item.getUserPhone());
        }

        return convertView;
    }

    class ViewHolder {
        TextView senderName;
        TextView senderAddress;
        TextView senderPhone;
        TextView expressInfoTV;
        TextView routerInfoTV;
        ImageView parcelTypeIV;
        ImageView expressLogoIV;
        View bottomLineV;
    }
}
